package it.uniroma2.exp.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class SRLSetsBuilder {

	/**
	 * @param args
	 * processed srl data files folder
	 * <bd|rc|all> for boundary definition or role classification or both
	 */
	public static void main(String[] args) {
		File baseFolder = null;
		File outputFolder = null;
		String type;
		try {
			type = args[1].toLowerCase();
			baseFolder = new File(args[0]);
			if (type.equalsIgnoreCase("bd") || type.equalsIgnoreCase("all")) {
				System.out.println("Building BD training & testing files...");
				outputFolder = new File(baseFolder, "bd");
				if (!outputFolder.exists())
					outputFolder.mkdirs();
				int count = 0;
				BufferedWriter bdTrain = new BufferedWriter(new FileWriter(new File(outputFolder, "bd_training.svm")));
				BufferedWriter bdTest = new BufferedWriter(new FileWriter(new File(outputFolder, "bd_testing.svm")));
				for (File file : baseFolder.listFiles()) {
					if (file.isDirectory())
						continue;
					System.out.println("Processing "+file.getName()+"...");
					BufferedReader in = new BufferedReader(new FileReader(file));
					String line = in.readLine();
					while (line != null) {
						if (line.startsWith("__NARG"))
							line = "-1\t"+line.substring(line.indexOf("|BT|"))+"\n";
						else
							line = "1\t"+line.substring(line.indexOf("|BT|"))+"\n";
						if (count < 10000)
							bdTrain.write(line);
						else if (count < 11000)
							bdTest.write(line);
						else break;
						count++;
						line = in.readLine();
					}
					in.close();
				}
				bdTrain.close();
				bdTest.close();
			}
			if (type.equalsIgnoreCase("rc") || type.equalsIgnoreCase("all")) {
				System.out.println("Building RC training & testing files...");
				outputFolder = new File(baseFolder, "rc");
				if (!outputFolder.exists())
					outputFolder.mkdirs();
				int countTr = 0;
				int countTs = 0;
				BufferedWriter rcTrain = new BufferedWriter(new FileWriter(new File(outputFolder, "rc_training.svm")));
				BufferedWriter rcTest = new BufferedWriter(new FileWriter(new File(outputFolder, "rc_testing.svm")));
				for (File file : baseFolder.listFiles()) {
					if (file.isDirectory())
						continue;
					System.out.println("Processing "+file.getName()+"...");
					BufferedReader in = new BufferedReader(new FileReader(file));
					String line = in.readLine();
					while (line != null) {
						if (!line.startsWith("__NARG")) {
							if (!file.getName().startsWith("24") && countTr < 10000) {
								rcTrain.write(line+"\n");
								countTr++;
							}
							else if (countTs < 1000) {
								rcTest.write(line+"\n");
								countTs++;
							}
							else break;
						}
						line = in.readLine();
					}
					in.close();
				}
				rcTrain.close();
				rcTest.close();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
